/*******************************************************************************
 *  Manik Hot Deploy
 *  Copyright (C) 2010 Ralph Soika  
 *  http://www.imixs.com
 *  
 *  This program is free software; you can redistribute it and/or 
 *  modify it under the terms of the GNU General Public License 
 *  as published by the Free Software Foundation; either version 2 
 *  of the License, or (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
 *  General Public License for more details.
 *  
 *  You can receive a copy of the GNU General Public
 *  License at http://www.gnu.org/licenses/gpl.html
 *  
 *  Contributors:  
 *  	Ralph Soika 
 * 
 *******************************************************************************/

package org.imixs.eclipse.manik;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.PropertyPage;

/**
 * Property Page to store the target folder from the app server
 * 
 * @author rsoika
 * 
 */
public class TargetPropertyPage extends PropertyPage {

	private static final String TARGET_TITLE = "Target Folder:";
	public static final String TARGET_PROPERTY = "TARGET";
	public static final String FILTER_PROPERTY = "FILTER";
	private static final String DEFAULT_TARGET = "/opt/glassfish-3.0.1/glassfish/domains/domain1/applications";

	private static final int TEXT_FIELD_WIDTH = 50;

	private Text targetText;
	private Button earButton;
	private Button warButton;
	private Button allButton;

	public TargetPropertyPage() {
		super();
	}

	
	public Shell getShell() {
		return super.getShell();
		
	}

	

	private void addTargetSection(Composite parent) {
		Composite composite = createDefaultComposite(parent);

		/* Filter Box */
		Label ownerLabel = new Label(composite, SWT.NONE);
		ownerLabel.setText("Filter:");

		Group group = new Group(composite, SWT.NONE);
		group.setLayout(new GridLayout(3, false));

		earButton = new Button(group, SWT.RADIO);
		earButton.setText("EAR");
		//earButton.setData("EAR");
		warButton = new Button(group, SWT.RADIO);
		warButton.setText("WAR");
		//warButton.setData("WAR");
		allButton = new Button(group, SWT.RADIO);
		allButton.setText("All Source Files");
		//allButton.setData("ALL");

		/* target Field */
		ownerLabel = new Label(composite, SWT.NONE);
		ownerLabel.setText(TARGET_TITLE);

		
		

		
		
		
		
		
		
		
		
		Group group2 = new Group(composite, SWT.NONE);
		
		group2.setLayout(new GridLayout(2, false));
		targetText = new Text(group2, SWT.SINGLE | SWT.BORDER);
		GridData gd = new GridData();
		gd.widthHint = convertWidthInCharsToPixels(TEXT_FIELD_WIDTH);
		targetText.setLayoutData(gd);

		// Populate owner text field
		try {
			String owner = ((IResource) getElement())
					.getPersistentProperty(new QualifiedName("",
							TARGET_PROPERTY));
			targetText.setText((owner != null) ? owner : DEFAULT_TARGET);
		} catch (CoreException e) {
			targetText.setText(DEFAULT_TARGET);
		}
		
		
		
		
		// Clicking the button will allow the user
	    // to select a directory
	    Button button = new Button(group2, SWT.PUSH);
	    button.setText("Browse...");
	    button.addSelectionListener(new SelectionAdapter() {
	      public void widgetSelected(SelectionEvent event) {
	        DirectoryDialog dlg = new DirectoryDialog(getShell());

	        // Set the initial filter path according
	        // to anything they've selected or typed in
	        dlg.setFilterPath(targetText.getText());

	        // Change the title bar text
	        dlg.setText("SWT's DirectoryDialog");

	        // Customizable message displayed in the dialog
	        dlg.setMessage("Select a directory");

	        // Calling open() will open and run the dialog.
	        // It will return the selected directory, or
	        // null if user cancels
	        String dir = dlg.open();
	        if (dir != null) {
	          // Set the text box to the new selection
	        	targetText.setText(dir);
	        }
	      }
	    });
		
		
		

		// Populate radio button
		try {
			earButton.setSelection(false);
			warButton.setSelection(false);
			allButton.setSelection(false);
			String filter = ((IResource) getElement())
					.getPersistentProperty(new QualifiedName("",
							FILTER_PROPERTY));
			if (filter == null || "".equals(filter))
				allButton.setSelection(true);
			else {
				if ("EAR".equals(filter))
					earButton.setSelection(true);
				else if ("WAR".equals(filter))
					warButton.setSelection(true);
				else
					allButton.setSelection(true);

			}

		} catch (CoreException e) {
			targetText.setText(DEFAULT_TARGET);
		}

	}

	/**
	 * @see PreferencePage#createContents(Composite)
	 */
	protected Control createContents(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		composite.setLayout(layout);
		GridData data = new GridData(GridData.FILL);
		data.grabExcessHorizontalSpace = true;
		composite.setLayoutData(data);

		addTargetSection(composite);
		return composite;
	}

	private Composite createDefaultComposite(Composite parent) {
		Composite composite = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		composite.setLayout(layout);

		GridData data = new GridData();
		data.verticalAlignment = GridData.FILL;
		data.horizontalAlignment = GridData.FILL;
		composite.setLayoutData(data);

		return composite;
	}

	protected void performDefaults() {
		// Populate the owner text field with the default value
		targetText.setText(DEFAULT_TARGET);
		allButton.setSelection(true);
		warButton.setSelection(false);
		earButton.setSelection(false);
	}

	public boolean performOk() {
		// store the value in the owner text field
		try {
			String target=targetText.getText();
			if (!target.endsWith("/"))
				target+="/";
			((IResource) getElement()).setPersistentProperty(new QualifiedName(
					"", TARGET_PROPERTY), target);

			String filter=null;
			if (earButton.getSelection())
				filter="EAR";
			else
				if (warButton.getSelection())
					filter="WAR";
				else filter="ALL";
			
			((IResource) getElement()).setPersistentProperty(new QualifiedName(
					"", FILTER_PROPERTY), filter);

				
		} catch (CoreException e) {
			return false;
		}
		return true;
	}

	
	

}
